package sys.task;

import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import sys.dao.CourseDao;
import sys.entity.StuScoreEntity;

import java.util.List;

@Slf4j
public class ScoreTask implements Job {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CourseDao courseDao;


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        Integer studentId = (Integer) redisTemplate.opsForList().rightPop("studentId");
        if (studentId != null) {
            //把该学生的各科成绩设置为0

            //1.查询有哪些科目
            List<Integer> courseIds = courseDao.getCourseId();
            log.info("courseIds:{}", courseIds);
            for (Integer courseId : courseIds) {
                //2.组装数据
                StuScoreEntity stuScoreEntity = new StuScoreEntity();
                stuScoreEntity.setStudentId(studentId);
                stuScoreEntity.setCourseId(courseId);
                stuScoreEntity.setScore(0);
                //3.插入到t_sc
                courseDao.addStuScore(stuScoreEntity);

            }


        }


    }
}
